<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{
   
   // 商品分类页
     public function getAdd()
      {
        // 获取分类信息
        $cates = DB::table('shop_cates')->get();
        
       return view('admin.cate.add',['cates'=>$cates]);
      }


      // 添加分类
    public function postInsert(Request $request)
    {   
        // 检测是否重复
        $this->validate($request,[
            'name'=>'unique:shop_cates,name'
            ],[
            'name.unique'=>'分类名称已经存在'
            ]);
        // 提取内容
        $data = $request->except('_token');
        // dd($data);
        // 拼接path
        // 判断是否为顶级分类
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            // 如果不是顶级分类获取父级分类的信息
            $p = DB::table('shop_cates')->where('id',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->id;
            
        }
            // dd($data);
        $res = DB::table('shop_cates')->insert($data); 

       // 提醒  
        if($res){
            return redirect('/admin/cate/index')->with('info','添加成功');
        }else{
            return redirect('/admin/cate/index')->with('info','添加失败');

        }
    
     } 

     public function getIndex(Request $request)
    {
        // 读取内容
         $res = DB::table('shop_cates')->select(DB::raw('*, concat(path,",",id) as paths'))
         ->orderBy('paths')->get();
         foreach($res as $k=>$v)
         {
            // dd($v);
            $total = count(explode(',',$v->path));
            // dd($total);
            $v->name = str_repeat('|-----',$total-1).$v->name;

         }
         // dd($res);
         //  分配

         return view('admin.cate.index',['cates'=>$res]);
         // dd($res); 
     } 
     public function getEdit(Request $request)
     {
        // dd(111);
        // 读取当前的分类信息
        $id = $request->input('id');
        $res = DB::table('shop_cates')->where('id',$id)->first();

        // dd($res);
        // 读取所有分类
        $cates = DB::table('shop_cates')->get(); 
        // 显示
        return view('admin.cate.edit',['cates'=>$cates,'info'=>$res]);
     }

    // 商品分类的修改
     public function getUpdate(Request $request)
     {
            // 获取数据
        $data = $request->except(['_token']);
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            $p = DB::table('shop_cates')->where('id',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->id;
        }
             $res = DB::table('shop_cates')->where('id',$data['id'])->update($data);
             // 提醒
             if($res){
                return redirect('/admin/cate/index')->with('info','修改成功');
             }else{
                return redirect('/admin/cate/index')->with('info','修改失败');

             }
     }

     public function getDelete(Request $request)
     {
        // 检测
        $id = $request->input('id');
        if(empty($id)) abrout(404);

        // 读取信息
        $info = DB::table('shop_cates')->where('id',$id)->first();
        // 拼接路径
        $path = $info->path.','.$info->id;
        // dd($id);  
        // 删除
        $res = DB::table('shop_cates')->where('id',$id)->delete();
        $res2 = DB::table('shop_cates')->where('path','like',$path.'%')->delete();

        if($res){
            return redirect('/admin/cate/index')->with('info','添加成功');
        }else{
            return redirect('/admin/cate/index')->with('info','添加失败');

        }

     }

      public static function getCateInfo()
    {
        // 读取内容
         $res = DB::table('shop_cates')->select(DB::raw('*, concat(path,",",id) as paths'))
         ->orderBy('paths')->get();
         foreach($res as $k=>$v)
         {
            // dd($v);
            $total = count(explode(',',$v->path));
            // dd($total);
            $v->name = str_repeat('|-----',$total-1).$v->name;

         }
         return $res;
    }
        
        // 递归获取分类信息
        public static function getAllCates($id)
        {
            $cates = DB::table('shop_cates')->where('pid',$id)->get();
            $arr = [];
            foreach($cates as $k=>$v){
                $v -> subcate = self::getAllCates($v->id);
                $arr[] = $v;
            }
            return $arr;
          
        }

        // public function test()
        // {
        //     $cates = self::getAllCates(0);
        //     // dd($cates);
        // }
 
}
